#include "dashboard.h"
#include <QPainter>
#include <QDebug>


#define FONT_SIZE 8

DashBoard::DashBoard(QWidget *parent)
    : QWidget(parent)
    , qDialEdgeColor(QColor(80,80,80))
    , qDialDivideBackgroudColor(QColor(20,20,20))
    , qDialEdgeSize(15)
    , qDialDivideSize(qDialEdgeSize + int(qDialEdgeSize * 1.5))
    , fMaxDivide(10)
    , fMinDivide(5)
    , BaseAngle(135.0)
    , fMaxValue(100)
    , qValue(0)
    , colorDivideRule(QColor(241,30,169))
    , colorCircle1(QColor(3,47,172))
    , colorCircle2(QColor(6,29,116))
    , colorCircle3(QColor(7,36,138))
    , colorCircle4(QColor(7,36,138))
    , colorCircle5(QColor(241,30,169))
    , colorCircle6(QColor(187,138,220))
    , colorPoniter(QColor(241,30,169))
    , colorFont(QColor(255,255,255))
{
    slot_ValueChanged(0);
    this->show();
    update();
}

void DashBoard::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);//抗锯齿
    painter.translate(width() >> 1, height() >> 1); // 将图像画在中心
    qRadius = (width() >> 2);//外圆形的半径
    qCircular = QPoint(0,0);
    drawDashBoard(&painter);
    drawDivideRule(&painter);
    drawDishBoardPointer(&painter);
    drawDashBoardText(&painter);
}

void DashBoard::drawDashBoard(QPainter *painter)
{
    painter->save();
    painter->setBrush(Qt::transparent);
    painter->fillRect(this->rect(), QColor(0, 0, 0, 0));

    QBrush qBrush = QBrush(colorCircle1);
    painter->setBrush(qBrush);//使用画刷
    painter->setPen(QColor(0,255,255));
    painter->drawEllipse(qCircular,qRadius,qRadius);//绘制最外层圆盘

    qBrush.setColor(colorCircle2);
    painter->setBrush(qBrush);
    painter->drawEllipse(qCircular,qRadius - qDialEdgeSize,qRadius - qDialEdgeSize);

    qBrush.setColor(colorCircle3);
    painter->setBrush(qBrush);
    painter->drawEllipse(qCircular,qRadius - qDialDivideSize-1,qRadius - qDialDivideSize-1);//绘制内层灰色圆盘


    qBrush.setColor(colorCircle4);
    painter->setBrush(qBrush);
    painter->setPen(colorCircle4);
    painter->drawEllipse(qCircular,30,30);//内部最大粉色圆盘

    qBrush.setColor(colorCircle5);
    painter->setBrush(qBrush);
    painter->drawEllipse(qCircular,15,15);//内部最大粉色圆盘

    qBrush.setColor(colorCircle6);
    painter->setBrush(qBrush);
    painter->drawEllipse(qCircular,12,12);//内部最大粉色圆盘

    painter->restore();
}

void DashBoard::drawDivideRule(QPainter *painter)
{
    painter->save();

    double fRadius = 0.0;
    //为了对称好看我们一般从逆时针90度位置开始绘制

    QPen qPen(colorDivideRule);
    qPen.setWidth(2);   //设置画笔的粗细
    painter->setPen(qPen);

    for(int uiLoop = 0; uiLoop < (fMaxDivide * fMinDivide) + 1 ;uiLoop++)
    {
        double fAngle = (BaseAngle + (static_cast<double>(270) / (fMaxDivide * fMinDivide)) * uiLoop);

        fRadius = (qRadius - 21);

        int x1Start = qCircular.x() + int(fRadius * cos(fAngle * 3.14 / 180));
        int y1Start = qCircular.y() + int(fRadius * sin(fAngle * 3.14 / 180));

        if(uiLoop % fMinDivide == 0)
        {
            fRadius = qRadius - qDialDivideSize + 1;
        }
        else
        {
            fRadius = qRadius - (qDialDivideSize / 1.3);
        }
        int x2End = qCircular.x() + int(fRadius * cos(fAngle * 3.14 / 180));
        int y2End =  qCircular.y() + int(fRadius * sin(fAngle * 3.14 / 180));

        painter->drawLine(QPoint(x1Start,y1Start),QPoint(x2End,y2End));
    }
    painter->restore();
}

void DashBoard::drawDishBoardPointer(QPainter *painter)
{
    painter->save();
//    painter->translate(qCircular); // 将图像画在中心
    QRect rect = QRect(-2,qDialDivideSize-qRadius,4,qRadius - qDialDivideSize-13);
    QPixmap pixmap = getSignalPixmap(colorPoniter);

    /* qValue = 0 指向中心点 即 0 -> 50 */
    painter->rotate(qValue);
    painter->drawPixmap(rect,pixmap);
    painter->restore();
}

void DashBoard::drawDashBoardText(QPainter *painter)
{
    //绘制表盘文字
    painter->save();
    painter->translate(qCircular);
    QPen qPen(colorFont);
    qPen.setWidth(2);   //设置画笔的粗细
    painter->setPen(qPen);
    QFont qFont("DejaVu Sans",FONT_SIZE+4,QFont::Bold,false);
    painter->setFont(qFont);
    painter->rotate(90);
    painter->translate(qRadius - qDialDivideSize - 20, 0);
    painter->rotate(-90);
    painter->drawText(QRect(-20,-10,40,25), Qt::AlignCenter, QString::number(mValue));
    painter->restore();

    painter->translate(qCircular); // 将图像画在中心

    for (int i = 0; i <= fMaxDivide; i++) {
        painter->save();
        painter->setPen(Qt::white);
        painter->setFont(QFont("DejaVu Sans", FONT_SIZE));
        painter->rotate(BaseAngle + (270.0 / (fMaxDivide)) * i);
        painter->translate(qRadius - qDialDivideSize - 10, 0);
        if(i == fMaxDivide/2)
            painter->rotate(90);
        else if (i < fMaxDivide/2) {
            painter->rotate(180);
        }
        painter->drawText(QRect(-20,-10,40,20), Qt::AlignCenter, QString::number((fMaxValue / fMaxDivide) * i));
        painter->restore();
    }
}

QPixmap DashBoard::getSignalPixmap(const QColor &color)
{
    QPixmap pixmap(4,qRadius - qDialDivideSize-13);
    pixmap.fill(QColor(0,0,0,0));
    QPainter painter(&pixmap);
    QBrush br(color);
    painter.setBrush(br);
    painter.setPen(color);
    QPoint p1 = QPoint(0, qRadius - qDialDivideSize-13);
    QPoint p2 = QPoint(4, qRadius - qDialDivideSize-13);
    QPoint p3 = QPoint(2,  0); //
    QPointF qTriangle[3] = {p1, p2, p3};

    painter.drawPolygon(qTriangle,3);
    return pixmap;

}

void DashBoard::slot_ValueChanged(int val)
{
    mValue = val;
    if(val <= 50)
    {
        val = 50 - val;
        qValue = -(int(static_cast<double>(270)/100*val));
    }
    else {
        val = val - 50;
        qValue = (int(static_cast<double>(270)/100*val));
    }
    update();
}


